package com.example.user_management_p1.service.impl;
import com.example.user_management_p1.service.IUserService;
import com.example.user_management_p1.service.ex.InsertException;
import com.pojo.Entity.User;
import com.pojo.mapper.UserMapper;
import com.pojo.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    RedisTemplate redisTemplate;
    private  static  final  String USER_KEY="User_Key";
    @Override
    public Integer insert(String uEmail,String uPassword,String CheckCode) {
        if(CheckCode==null||uEmail==null)
            throw new InsertException("邮箱和验证码不能为空");
        String code= String.valueOf(redisTemplate.opsForValue().get(uEmail+":CheckCode"));
        if(code==null||!code.equals(CheckCode))
            throw new InsertException("邮箱或验证码错误");
        User user=new User();
        user.setUEmail(uEmail);
        user.setUPassword(uPassword);
        user.setUSalt(String.valueOf(UUID.randomUUID()).toUpperCase());
        user.setUPassword(Tool.MD5(user.getUSalt(),user.getUPassword()));
        user.setUType(5);
        Integer count=0;
        try {
            count=userMapper.insert(user);
            redisTemplate.opsForHash().put(USER_KEY,uEmail,user);
            redisTemplate.opsForHash().getOperations().expire(USER_KEY,120,TimeUnit.MINUTES);
        }catch (Exception e){
            throw new InsertException("添加异常");
        }
        return count;
    }

    @Override
    public String login(String uEmail, String uPassword) {
        String identifying=null;
        User user=userMapper.findByUem(uEmail);
        String passwordMD5=Tool.MD5(user.getUSalt(),uPassword);
        if(passwordMD5.equals(user.getUPassword())){
             identifying= String.valueOf(UUID.randomUUID());
            redisTemplate.opsForHash().put(identifying,"uID",user.getUID());
            redisTemplate.opsForHash().put(identifying,"uType",user.getUType());
            redisTemplate.opsForHash().getOperations().expire(identifying,30,TimeUnit.MINUTES);
        }
        return identifying;
    }

    @Override
    public Integer updatePassword(String newPassword, String oldPassword, HttpServletRequest httpServletRequest) {
        String identifying=httpServletRequest.getHeader("identifying");
        Integer uID= (Integer) redisTemplate.opsForHash().get(identifying,"uID");
        User user=userMapper.findByUId(uID);
        String passwordMD5=Tool.MD5(user.getUSalt(),oldPassword);
        if(passwordMD5.equals(user.getUPassword())){
            passwordMD5=Tool.MD5(user.getUSalt(), newPassword);
            return userMapper.updatePassword(passwordMD5,user.getUID());
        }
        return 0;
    }

    @Override
    public Integer updateType(Integer uID, Integer uType) {
        return userMapper.updateType(uID,uType);
    }

}
